package com.woniuxy.redis01.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.redis01.entity.Stu;
import com.woniuxy.redis01.mapper.StuMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @Author author
 * @Create 2023-09-28 11:39
 */
@Component
public class MyConsumer {

    @Autowired
    StuMapper stuMapper;

    //这个注解就是让这个方法监听指定的队列，接收消息，处理消息
    //String str,   消息体，可以写Object
    // Message message, 完整的消息对象,保护其他信息
    // Channel channel,信道对象
    @RabbitListener(queues="queueorder")
    public void receiveQueueorder(Stu stu, Message message, Channel channel) throws IOException {
       stuMapper.insertStu(stu);
       channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(queues="queueorderdlx")
    public void receiveQqueueorderdlx(String str, Message message, Channel channel) throws IOException {
        System.out.println("10s以后查询订单的状态，没有支付，修改成不能支付的状态："+new Date());
       channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(queues="queuedelayplugin")
    public void receivequeuedelayplugin(String str, Message message, Channel channel) throws IOException {
        System.out.println("延迟插件10s以后处理消息："+new Date());
        System.out.println(message.getMessageProperties().getHeader("h1").toString());
        System.out.println(message.getMessageProperties().getHeader("h2").toString());
        System.out.println(str);
       channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
